# Copyright (c) 2011 CESNET
#
# LICENSE TERMS
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 3. Neither the name of the Company nor the names of its contributors
#    may be used to endorse or promote products derived from this
#    software without specific prior written permission.
#
# ALTERNATIVELY, provided that this notice is retained in full, this
# product may be distributed under the terms of the GNU General Public
# License (GPL) version 2 or later, in which case the provisions
# of the GPL apply INSTEAD OF those given above.
#
# This software is provided ``as is'', and any express or implied
# warranties, including, but not limited to, the implied warranties of
# merchantability and fitness for a particular purpose are disclaimed.
# In no event shall the company or contributors be liable for any
# direct, indirect, incidental, special, exemplary, or consequential
# damages (including, but not limited to, procurement of substitute
# goods or services; loss of use, data, or profits; or business
# interruption) however caused and on any theory of liability, whether
# in contract, strict liability, or tort (including negligence or
# otherwise) arising in any way out of the use of this software, even
# if advised of the possibility of such damage.
#

NAME = @PACKAGE_NAME@
VERSION = $(shell cut -f1 ./VERSION | tr -d '\n')
RELEASE = @RELEASE@

# Various configurable paths (remember to edit Makefile.in, not Makefile)
prefix = @prefix@
exec_prefix = @exec_prefix@
datarootdir = @datarootdir@
datadir = @datadir@
bindir = @bindir@
includedir = @includedir@
libdir =  @libdir@
mandir = @mandir@
sysconfdir = @sysconfdir@
dbusconfdir = @DBUSCONF@
dbusservices = @DBUSSERVICES@

TOPDIR = @CONFIGURE_PWD@
CC = @CC@
DOXYGEN = @DOXYGEN@
DOXYGEN_CONFIG = Doxyfile
DOXYGEN_DIR = doxygen
RPMBUILD = @RPMBUILD@
INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL@ -m 644 -o @USER@ -g @GROUP@
RPMDIR = RPMBUILD

AGENT = netopeer-agent
AGENT_LIBS = @AGENT_LIBS@
AGENT_SRCS =  src/agent.c \
	src/agent_comm.c \
	@AGENT_SRCS_COMM@
AGENT_HDRS = src/comm.h \
	@AGENT_HDRS_COMM@
AGENT_OBJS = $(AGENT_SRCS:%.c=$(OBJDIR)/%.o)

SERVER = netopeer-server
SERVER_LIBS = @SERVER_LIBS@
SERVER_SRCS =  src/server.c \
	src/server_operations.c \
	src/cfgnetopeer-transapi.c \
	@SERVER_SRCS_COMM@
SERVER_HDRS = src/comm.h \
	src/server_operations.h \
	@SERVER_HDRS_COMM@
SERVER_OBJS = $(SERVER_SRCS:%.c=$(OBJDIR)/%.o)

CFLAGS = @CFLAGS@
CPPFLAGS = @CPPFLAGS@
DEFINE = -DMODULES_CFG_DIR=\"$(DESTDIR)/etc/netopeer/modules.conf.d/\" -DVERSION=\"$(VERSION)\"

OBJDIR= .obj
SUBDIRS = dm_manager

CFGS =	config/datastore.xml \
	config/netopeer-cfgnetopeer.yin \
	config/netopeer-cfgnetopeer-data.rng \
	config/netopeer-cfgnetopeer-gdefs.rng \
	config/netopeer-cfgnetopeer-schematron.xsl
	
CFGS_TAR = $(CFGS) \
	config/org.liberouter.netopeer.conf.in \
	config/org.liberouter.netopeer.server.service.in

all: $(AGENT) $(SERVER) $(SUBDIRS)

config/datastore.xml:
	touch $@

.PHONY: $(SUBDIRS)
$(SUBDIRS):
	@$(MAKE) -C $@

$(SERVER): $(SERVER_OBJS)
	@rm -f $@;
	$(CC) $(CFLAGS) $(CPPFLAGS) $(SERVER_OBJS) $(SERVER_LIBS) -o $@;

$(AGENT): $(AGENT_OBJS)
	@rm -f $@;
	$(CC) $(CFLAGS) $(CPPFLAGS) $(AGENT_OBJS) $(AGENT_LIBS) -o $@;

$(OBJDIR)/%.o: %.c
	@[ -d $$(dirname $@) ] || \
		(mkdir -p $$(dirname $@))
	$(CC) $(CFLAGS) $(CPPFLAGS) $(DEFINE) -fPIC -c $< -o $@

.PHONY: clean
clean:
	for i in $(SUBDIRS) ; do $(MAKE) -C $$i clean ; done
	rm -rf $(AGENT) $(SERVER) $(OBJDIR) doxygen/

.PHONY: doc
doc: $(DOXYGEN_CONFIG)
	@if [ -n "$(DOXYGEN)" ]; then \
		(cat $(DOXYGEN_CONFIG); \
		echo 'OUTPUT_DIRECTORY = $(DOXYGEN_DIR)' ) | \
		$(DOXYGEN) - > /dev/null 2>&1; \
	else \
		echo "Missing doxygen"; \
		exit 1; \
	fi

.PHONY: dist
dist: $(NAME).spec tarball rpm

.PHONY: tarball
tarball: $(SERVER_SRCS) $(AGENT_SRCS) $(SERVER_HDRS) $(AGENT_HDRS)
	@rm -rf $(NAME)-$(VERSION);
	@mkdir $(NAME)-$(VERSION);
	@for i in $(SUBDIRS) ; do $(MAKE) -C $$i tarball-prepare; done; \
	for i in $(SERVER_SRCS) $(AGENT_SRCS) $(SERVER_HDRS) $(AGENT_HDRS) $(CFGS_TAR) configure.in configure \
	    Makefile.in VERSION Doxyfile $(NAME).spec.in netopeer.rc.in install-sh; do \
	    [ -d $(NAME)-$(VERSION)/$$(dirname $$i) ] || (mkdir -p $(NAME)-$(VERSION)/$$(dirname $$i)); \
		cp $$i $(NAME)-$(VERSION)/$$i; \
	done;
	@rm -rf $(RPMDIR)/SOURCES/; \
	mkdir -p $(RPMDIR)/SOURCES/; \
	tar -c -z -f $(RPMDIR)/SOURCES/$(NAME)-$(VERSION)-$(RELEASE).tar.gz \
	    $(NAME)-$(VERSION); \
	rm -rf $(NAME)-$(VERSION);

.PHONY: rpm
rpm: tarball
	mkdir -p $(RPMDIR)/BUILD $(RPMDIR)/RPMS $(RPMDIR)/SRPMS
	$(RPMBUILD) -ba $(NAME).spec \
		--define "_topdir `pwd`/$(RPMDIR)";

.PHONY: cleanall
cleanall: clean distclean

.PHONY: distclean
distclean:
	rm -rf $(RPMDIR)

.PHONY: install
install: $(SERVER) $(AGENT) $(CFGS)
	$(INSTALL) -d $(DESTDIR)/$(bindir);
	$(INSTALL_PROGRAM) $(SERVER) $(DESTDIR)/$(bindir)/;
	$(INSTALL_PROGRAM) $(AGENT) $(DESTDIR)/$(bindir)/;
	for i in $(SUBDIRS) ; do \
		$(MAKE) DESTDIR=$(patsubst .%,$(TOPDIR)/.%,$(DESTDIR)) -C $$i install \
		|| exit 1; \
	done
	if test -n "$(dbusconfdir)"; then \
		$(INSTALL) -d $(DESTDIR)/$(dbusconfdir)/; \
		$(INSTALL_DATA) config/org.liberouter.netopeer.conf $(DESTDIR)/$(dbusconfdir)/; \
	fi
	if test -n "$(dbusservices)"; then \
		$(INSTALL) -d $(DESTDIR)/$(dbusservices)/; \
		$(INSTALL_DATA) config/org.liberouter.netopeer.server.service $(DESTDIR)/$(dbusservices)/; \
	fi
	$(INSTALL) -d $(DESTDIR)/$(sysconfdir)/netopeer/cfgnetopeer/
	for i in $(CFGS); do \
		$(INSTALL_DATA) $$i $(DESTDIR)/$(sysconfdir)/netopeer/cfgnetopeer/; \
	done
	$(INSTALL) -d $(DESTDIR)/$(sysconfdir)/init.d/;
	$(INSTALL_PROGRAM) -m 755 netopeer.rc $(DESTDIR)/$(sysconfdir)/init.d/
	$(INSTALL) -d $(DESTDIR)/$(sysconfdir)/netopeer/modules.conf.d/;
	dm_manager/netopeer-dm-manager add -n Netopeer -c urn:cesnet:tmc:netopeer:1.0 \
		-d $(DESTDIR)/$(sysconfdir)/netopeer/cfgnetopeer/netopeer-cfgnetopeer.yin \
		-r file $(DESTDIR)/$(sysconfdir)/netopeer/cfgnetopeer/datastore.xml \
		-f $(DESTDIR)/$(sysconfdir)/netopeer/modules.conf.d/


